package com.aisile.admin.service.impl;

import com.aisile.admin.mapper.AdSensitiveMapper;
import com.aisile.admin.service.AdSensitiveService;
import com.aisile.model.admin.dtos.SensitiveDto;
import com.aisile.model.admin.pojos.AdChannel;
import com.aisile.model.admin.pojos.AdSensitive;
import com.aisile.model.common.dtos.PageResponseResult;
import com.aisile.model.common.dtos.ResponseResult;
import com.aisile.model.common.enums.AppHttpCodeEnum;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import  com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.apache.directory.shared.kerberos.codec.hostAddresses.actions.AddHostAddress;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper,AdSensitive> implements AdSensitiveService {

    @Override
    public ResponseResult findAll(SensitiveDto adSensitive) {
        if (adSensitive == null) {
            // 提示错误
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 初始化分页参数  dto 为什么不用new，因为在controller层 json转对象 已经帮着new
        adSensitive.checkParam();
        // 开始拼接条件
        LambdaQueryWrapper<AdSensitive> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //StringUtils.isNotEmpty();  isNotEmpty()方法会将空字符进行判断，有空字符也会判断为不空-true；
        //StringUtils.isNotBlank();  而isNotBlank()方法只对非空字符进行判断。
        lambdaQueryWrapper.like(StringUtils.isNotBlank(adSensitive.getName()),AdSensitive::getSensitives, adSensitive.getName());
        // 分页
        Page page = new Page(adSensitive.getPage(), adSensitive.getSize());
        IPage<AdSensitive> iPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
        //  ((Long)iPage.getTotal()).intValue()
        ResponseResult responseResult = new PageResponseResult(adSensitive.getPage(), adSensitive.getSize(), Integer.valueOf(iPage.getTotal() + ""));
        responseResult.setData(iPage.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult add(int id) {
          this.getById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update( AdSensitive adSensitive) {
        //1.参数检查
        if(adSensitive == null || adSensitive.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.修改
        updateById(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }

    @Override
    public ResponseResult delete(int id) {
//        if(id == null){
//            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
//        }
        //2.判断当前敏感词是否存在
        AdSensitive adSensitive = getById(id);
        if(adSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //3.删除操作
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }



    @Override
    public ResponseResult save1(AdSensitive adSensitive) {
        if(adSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.保存
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delMany1(List<Integer> list) {
      this.baseMapper.selectPage1(list);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
//    @Override
//    public ResponseResult delMany1() {
//        this.AdSensitive();
//        return null;
//    }


}
